package com.ruoyi.project.poem.poemsetdetail.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.common.utils.text.Convert;
import com.ruoyi.framework.aspectj.lang.annotation.DataSource;
import com.ruoyi.framework.aspectj.lang.enums.DataSourceType;
import com.ruoyi.project.poem.poem.domain.Poem;
import com.ruoyi.project.poem.poem.mapper.PoemMapper;
import com.ruoyi.project.poem.poemsetdetail.domain.PoemSetDetail;
import com.ruoyi.project.poem.poemsetdetail.mapper.PoemSetDetailMapper;
import com.ruoyi.project.poem.poemsetdetail.service.IPoemSetDetailService;

/**
 * 诗单详情Service业务层处理
 * 
 * @author shixiangwen
 * @date 2020-03-30
 */
@Service
@DataSource(DataSourceType.POEM)
public class PoemSetDetailServiceImpl implements IPoemSetDetailService {

	@Autowired
	private PoemSetDetailMapper poemSetDetailMapper;

	@Autowired
	PoemMapper poemMapper;

	/**
	 * 查询诗单详情
	 * 
	 * @param id 诗单详情ID
	 * @return 诗单详情
	 */
	@Override
	public PoemSetDetail selectPoemSetDetailById(Long id) {
		return poemSetDetailMapper.selectPoemSetDetailById(id);
	}

	@Override
	public Integer selectMaxSort(Long setId) {
		Integer sort = poemSetDetailMapper.selectMaxSort(setId);
		return sort == null ? 0 : sort;
	}

	/**
	 * 查询诗单详情列表
	 * 
	 * @param poemSetDetail 诗单详情
	 * @return 诗单详情
	 */
	@Override
	public List<PoemSetDetail> selectPoemSetDetailList(PoemSetDetail poemSetDetail) {
		List<PoemSetDetail> list = poemSetDetailMapper.selectPoemSetDetailList(poemSetDetail);
		list.forEach(item -> {
			Poem poem = poemMapper.selectPoemById(item.getPoemId());
			if (poem != null) {
				item.setMingcheng(poem.getMingcheng());
				item.setZuozhe(poem.getZuozhe());
			}
		});
		return list;
	}

	/**
	 * 新增诗单详情
	 * 
	 * @param poemSetDetail 诗单详情
	 * @return 结果
	 */
	@Override
	public int insertPoemSetDetail(PoemSetDetail poemSetDetail) {
		poemSetDetail.setCreateTime(DateUtils.getNowDate());
		poemSetDetail.setCreateBy(ShiroUtils.getLoginName());
		return poemSetDetailMapper.insertPoemSetDetail(poemSetDetail);
	}

	/**
	 * 修改诗单详情
	 * 
	 * @param poemSetDetail 诗单详情
	 * @return 结果
	 */
	@Override
	public int updatePoemSetDetail(PoemSetDetail poemSetDetail) {
		poemSetDetail.setUpdateTime(DateUtils.getNowDate());
		poemSetDetail.setUpdateBy(ShiroUtils.getLoginName());
		return poemSetDetailMapper.updatePoemSetDetail(poemSetDetail);
	}

	/**
	 * 删除诗单详情对象
	 * 
	 * @param ids 需要删除的数据ID
	 * @return 结果
	 */
	@Override
	public int deletePoemSetDetailByIds(String ids) {
		return poemSetDetailMapper.deletePoemSetDetailByIds(Convert.toStrArray(ids));
	}

	/**
	 * 删除诗单详情信息
	 * 
	 * @param id 诗单详情ID
	 * @return 结果
	 */
	@Override
	public int deletePoemSetDetailById(Long id) {
		return poemSetDetailMapper.deletePoemSetDetailById(id);
	}
}
